<?php
/* 
 *客户收款（订单）
 */
class ReceiptAction extends TransAction {
    public function _filter(&$map) {
        $key = trim($_REQUEST['keywords']);
        if(!empty($key)) {
            $where['Product.name'] = array('like',"%".$key."%");//产品名称
            $where['Order_detail.mano'] = array('like',"%".$key."%");
            $where['Sale.po'] = array('like',"%".$key."%");
            $where['Product.cas'] = array('like',"%".$key."%");
            $where['Product.ename'] = array('like',"%".$key."%");
            $where['Customer.name'] = array('like',"%".$key."%");
            $where['_logic'] = "or";
            $map['_complex'] = $where;

        }
        $map['Order_detail.status'] = array('neq',0); //待确认的订单不显示

        if(!empty($_REQUEST['customer']))
            $map['_string'] = "(Customer.name like '%".$_REQUEST['customer']."%') or (Customer.company like '%".$_REQUEST['customer']."%')";


        if(isset($_REQUEST['dreceipt_status']) && $_REQUEST['dreceipt_status'] != '' )
            $map['Order_detail.receipt_status'] = $_REQUEST['dreceipt_status'];

        if(isset($_REQUEST['dstatus']) && $_REQUEST['dstatus'] != '')
            $map['Order_detail.status'] = $_REQUEST['dstatus'];

         //time 
        $cond_time = array();
        if( $_REQUEST['start_time'] != '' )
          array_push($cond_time, array( 'egt' , strtotime($_REQUEST['start_time']) ) );
          //$cond_time = array( 'egt' , strtotime($_REQUEST['start_time']) );
        if( $_REQUEST['end_time'] != '' )
          array_push($cond_time, array( 'elt' , strtotime($_REQUEST['end_time']) ) );

        //选择了时间类型,并且时间不为空，则按时间进行搜索
        if( $_REQUEST['time_type'] != "" && 
          ($_REQUEST['end_time'] != '' ||  $_REQUEST['start_time'] != '' ) ){
            switch ($_REQUEST['time_type']) {
              case '1': 
                $map['Order_detail.first_pay_time'] = $cond_time;
                break;
              case '2': 
                $map['Order_detail.remain_pay_time'] = $cond_time;
                break;
              case '3': 
                $map['Order_detail.back_money_time'] = $cond_time;
                break;
              case '4': //交货期
                $map['Order_detail.delivery_date '] = $cond_time;
                break;
              case '5': //创建时间
                $map['Order_detail.create_time '] = $cond_time;
                break;
              
              default:  //按下单时间查询
                $map['Order_detail.first_pay_time '] = $cond_time;
                break;
            }
            
        }

    }

    public function  index() {
        parent::index('OrderView');
    }

    public function edit(){
        $model = M ( 'Order_detail');
        $id = $_REQUEST [$model->getPk ()];
        if(isset($id)){
            $vo = $model->where('id = '.$id )->find();
            $this->assign ( 'vo', $vo );
            $this->display ();
        }
    }
	//财务复核
    public function check(){
        $model = M ( 'Order_detail');
        $id = $_REQUEST [$model->getPk ()];
        if(isset($id)){
            $vo = $model->where('id = '.$id )->find();
            $this->assign ( 'vo', $vo );
            $this->display ();
        }
    }

     public function update()
    {   
	$old_value = M('Order_detail')->getById($_POST['id']);
	//实际付款＝ 预付款＋余款 － 退款
	if(!empty( $_POST['first_pay'] )){
		$_POST['real_pay'] = $_POST['firt_pay']+$old_value['remain_pay']-$old_value['back_money'];
	}
	else if(!empty( $_POST['remain_pay'] )){
		$_POST['real_pay'] = $_POST['remain_pay']+$old_value['first_pay']-$old_value['back_money'];
	}
	else if(!empty( $_POST['back_money'] )){
		$_POST['real_pay'] = $old_value['remain_pay']+$old_value['first_pay']-$_POST['back_money'];
	}

        A('Order_detail')->update();
    }

     public function reset_pay(){
        M('Order_detail')->where('id = '.$_REQUEST['id'])->setField($_REQUEST['field'],0);
        $this->success("财务值重新归为0！");
    }
     //等待退款
     public function reback_money(){
        M('Order_detail')->where('id = '.$_REQUEST['id'])->setField($_REQUEST['field'],3);
        $this->success("成功设订单为等待退款中！");
    }
    
}
?>
